/**
 * 极简验证程序 - 快速检验多源数据聚合系统是否工作
 */
public class QuickCheck {
    
    public static void main(String[] args) {
        System.out.println("=== 多源数据聚合系统验证 ===");
        
        try {
            // 1. 初始化
            UserPreferences userPrefs = new UserPreferences();
            userPrefs.setPreferredUnits("metric");
            Location location = new Location("Beijing");
            
            MultiAPIManager apiManager = new MultiAPIManager(userPrefs);
            
            // 2. 注册服务
            WeatherAPIManager openWeather = new WeatherAPIManager(userPrefs);
            WeatherAPIComService weatherAPI = new WeatherAPIComService(userPrefs);
            
            apiManager.registerAPIService(openWeather);
            apiManager.registerAPIService(weatherAPI);
            
            System.out.println("✓ 已注册 " + apiManager.getRegisteredServices().size() + " 个API服务");
            
            // 3. 检查服务状态
            for (WeatherAPIService service : apiManager.getRegisteredServices()) {
                boolean available = service.isServiceAvailable();
                System.out.println("✓ " + service.getDisplayName() + ": " + 
                    (available ? "可用" : "不可用"));
            }
            
            // 4. 测试获取天气数据
            System.out.println("\n=== 测试数据获取 ===");
            
            long startTime = System.currentTimeMillis();
            WeatherData weather = apiManager.getCurrentWeather(location);
            long responseTime = System.currentTimeMillis() - startTime;
            
            if (weather != null) {
                System.out.println("✓ 成功获取天气数据");
                System.out.println("  城市: " + location.getCity());
                System.out.println("  温度: " + weather.getTemperature() + "°C");
                System.out.println("  湿度: " + weather.getHumidity() + "%");
                System.out.println("  描述: " + weather.getDescription());
                System.out.println("  响应时间: " + responseTime + "ms");
                
                // 5. 测试缓存
                System.out.println("\n=== 测试缓存 ===");
                long cacheStart = System.currentTimeMillis();
                WeatherData cachedWeather = apiManager.getCurrentWeather(location);
                long cacheTime = System.currentTimeMillis() - cacheStart;
                
                if (cachedWeather != null) {
                    System.out.println("✓ 缓存机制工作正常");
                    System.out.println("  缓存响应时间: " + cacheTime + "ms");
                    System.out.println("  加速比: " + (responseTime / (double)cacheTime) + "x");
                }
                
                // 6. 显示可靠性统计
                System.out.println("\n=== 可靠性统计 ===");
                WeatherDataCache cache = apiManager.getCache();
                
                for (WeatherAPIService service : apiManager.getRegisteredServices()) {
                    String sourceId = service.getSourceId();
                    double score = cache.getSourceReliabilityScore(sourceId);
                    WeatherDataCache.SourceReliabilityStats stats = cache.getSourceStats(sourceId);
                    
                    System.out.println("✓ " + service.getDisplayName() + " 可靠性: " + 
                        String.format("%.2f", score));
                    
                    if (stats != null) {
                        System.out.println("  成功: " + stats.getSuccessCount() + 
                                         ", 失败: " + stats.getFailureCount() +
                                         ", 延迟: " + String.format("%.0f", stats.getAverageLatency()) + "ms");
                    }
                }
                
                System.out.println("\n✓ 多源数据聚合系统验证成功！");
                
            } else {
                System.out.println("✗ 无法获取天气数据");
            }
            
            // 清理
            apiManager.shutdown();
            
        } catch (Exception e) {
            System.err.println("✗ 验证失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
} 